package com.newbsd.web.maventest.servlet;

import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.newbsd.web.maventest.util.AuthCode;

/**
 * @author 张成玉-验证码servlet
 * Servlet implementation class AuthCodeServlet
 */
public class AuthCodeServlet extends HttpServlet {
	
    public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String authCode = AuthCode.getAuthCode();  
        response.setCharacterEncoding("UTF-8");
        response.setContentType("image/jpeg");
        request.getSession().setAttribute("authCode", authCode);   //张成玉-将验证码保存到session中，便于以后验证  
          
        try {  
            //张成玉-发送图片  
            ImageIO.write(AuthCode.getAuthImg(authCode), "JPEG", response.getOutputStream());  
        } catch (IOException e){  
            e.printStackTrace();  
        }  
	}

}
